package cn.jinjimi.app.custom.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.base.util.DateUtils;
import cn.jinjimi.base.util.ViewUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.entity.Follow;
import cn.jinjimi.app.custom.ui.activity.AddFollowRecordActivity;
import cn.jinjimi.framework.router.Router;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;

/**
 * Created by Roy on 16/6/28.
 */
public class CustomArchivesAdapter extends MyBaseAdapter<Follow> {

    public final static int TYPE_PHOTO = 0; // 图片跟进记录
    public final static int TYPE_POLICY = 1; // 保单跟进记录
    public final static int TYPE_COUNT = 2;

    private Context mContext;
    private Custom mCustom;

    public CustomArchivesAdapter(Context context) {
        this.mContext = context;
    }

    public void setCustom(Custom custom) {
        if (custom != null)
            this.mCustom = custom;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        Follow item = getItem(position);
        if (item.attachments == null || item.attachments.size() == 0)
            return TYPE_PHOTO;

        if (item.attachments.get(0).mediaType.equals("image"))
            return TYPE_PHOTO;

        if (item.attachments.get(0).mediaType.equals("html"))
            return TYPE_POLICY;

        return TYPE_PHOTO;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        int type = getItemViewType(position);

        if (convertView == null || null == convertView.getTag()) {
            switch (type) {
                case TYPE_PHOTO:
                    convertView = View.inflate(mContext, R.layout.item_custom_follow_photo, null);
                    holder = new PhotoHolder(convertView);
                    break;
                case TYPE_POLICY:
                    convertView = View.inflate(mContext, R.layout.item_custom_follow_policy, null);
                    holder = new PolicyHolder(convertView);
                    break;
            }
            convertView.setTag(holder);
        } else {
            switch (type) {
                case TYPE_PHOTO:
                    holder = (PhotoHolder) convertView.getTag();
                    break;
                case TYPE_POLICY:
                    holder = (PolicyHolder) convertView.getTag();
                    break;
            }
        }

        holder.setData(position);

        return convertView;
    }

    public class Holder {
        Follow item;

        RelativeLayout llDate;
        TextView tvDate;
        TextView tvTime;
        TextView tvSecDate;
        protected TextView tvContent;
        View viewBottom;

        public Holder(View view) {
            llDate = (RelativeLayout) view.findViewById(R.id.ll_date);
            tvDate = (TextView) view.findViewById(R.id.tv_date);
            tvSecDate = (TextView) view.findViewById(R.id.tv_sec_date);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            tvContent = (TextView) view.findViewById(R.id.tv_content);
            viewBottom = view.findViewById(R.id.view_bottom);
        }

        public void setData(int position) {
            item = getItem(position);

            tvDate.setText(DateUtils.dealWithDataInCustomDay(item.time));
            tvTime.setText(DateUtils.formatHHmm(item.time));
            tvSecDate.setText(DateUtils.formatMonthYear(item.time));
            tvContent.setText(item.content);

            if (position == 0) {
                llDate.setVisibility(View.VISIBLE);
            } else {
                Follow lastItem = getItem(position - 1);
                llDate.setVisibility(DateUtils.isSameDay(lastItem.time, item.time) ? View.GONE : View.VISIBLE);
            }

            if (position == getCount() - 1) {
                ViewUtils.setHeight(viewBottom, mContext.getResources().getDimensionPixelOffset(R.dimen.dp_30));
            } else {
                ViewUtils.setHeight(viewBottom, mContext.getResources().getDimensionPixelOffset(R.dimen.dp_10));
            }

        }
    }

    public class PhotoHolder extends Holder {
        ImageView btnEdit;
        GridView gridView;

        public PhotoHolder(View view) {
            super(view);

            btnEdit = (ImageView) view.findViewById(R.id.btn_edit);
            gridView = (GridView) view.findViewById(R.id.photo_gridview);
        }

        @Override
        public void setData(int position) {
            super.setData(position);

            if (item.attachments == null || item.attachments.size() == 0) {
                gridView.setVisibility(View.GONE);
            } else {
                gridView.setVisibility(View.VISIBLE);

                CustomArchivesPhotosAdapter gridViewAdapter = new CustomArchivesPhotosAdapter(mContext);
                gridViewAdapter.setItems(item.attachments);
                gridView.setAdapter(gridViewAdapter);
            }
            btnEdit.setVisibility(item.channel == 1 ? View.VISIBLE : View.GONE);
            if (btnEdit.getVisibility() == View.VISIBLE) {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tvContent.getLayoutParams();
                params.setMargins(mContext.getResources().getDimensionPixelOffset(R.dimen.dp_15), mContext.getResources().getDimensionPixelOffset(R.dimen.dp_13), 0, 0);
                tvContent.setLayoutParams(params);
            } else {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tvContent.getLayoutParams();
                params.setMargins(mContext.getResources().getDimensionPixelOffset(R.dimen.dp_15), mContext.getResources().getDimensionPixelOffset(R.dimen.dp_13), mContext.getResources().getDimensionPixelOffset(R.dimen.dp_15), 0);
                tvContent.setLayoutParams(params);
            }

            btnEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AddFollowRecordActivity.launch(mContext, mCustom, false, false, item);
                }
            });
        }
    }

    public class PolicyHolder extends Holder {

        TextView tvPolicy;
        LinearLayout llPolicy;

        public PolicyHolder(View view) {
            super(view);
            tvPolicy = (TextView) view.findViewById(R.id.tv_policy);
            llPolicy = (LinearLayout) view.findViewById(R.id.ll_policy);
        }

        @Override
        public void setData(int position) {
            super.setData(position);
            if (item.attachments != null && item.attachments.size() > 0) {
                tvPolicy.setText(item.attachments.get(0).name);
                llPolicy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Router.create(mContext, item.attachments.get(0).imageUrl).open();
                    }
                });
            }
        }
    }
}